/* p5_8.c */
/* square wave signal should be fed to PB6 pin. Make sure it is 3.3 to 5V peak-to-peak. Initialize Timer0A in edge-time mode to capture rising edges. The input pin of Timer0A is PB6. See Table 5-2.*/

void Timer0Capture_init(void)
{
    SYSCTL->RCGCTIMER |= 1;     /* enable clock to Timer Block 0 */
    SYSCTL->RCGCGPIO |= 2;      /* enable clock to PORTB */

    GPIOB->DIR &= ~0x40;        /* make PB6 an input pin */
    GPIOB->DEN |= 0x40;         /* make PB6 as digital pin */
    GPIOB->AFSEL |= 0x40;       /* use PB6 alternate function */
    GPIOB->PCTL &= ~0x0F000000;  /* configure PB6 for T0CCP0 */
    GPIOB->PCTL |= 0x07000000;
    
    TIMER0->CTL &= ~1;          /* disable timer0A during setup */
    TIMER0->CFG = 4;            /* 16-bit timer mode */
    TIMER0->TAMR = 0x17;        /* up-count, edge-time, capture mode */
    TIMER0->CTL &= ~0x0C;        /* capture the rising edge */
    TIMER0->CTL |= 1;           /* enable timer0A */
}

/* This function captures two consecutive rising edges of a periodic signal from Timer Block 0 Timer A and returns the time difference (the period of the signal). */
int Timer0A_periodCapture(void)
{
    int lastEdge, thisEdge;
    
    /* capture the first rising edge */
    TIMER0->ICR = 4;            /* clear timer0A capture flag */
    while((TIMER0->RIS & 4) == 0) ;    /* wait till captured */
    lastEdge = TIMER0->TAR;     /* save the timestamp */

    /* capture the second rising edge */
    TIMER0->ICR = 4;            /* clear timer0A capture flag */
    while((TIMER0->RIS & 4) == 0) ;    /* wait till captured */
    thisEdge = TIMER0->TAR;     /* save the timestamp */
    
    return (thisEdge â€“ lastEdge) & 0x00FFFFFF; /* return the time difference */
}